package cappuccino.couldbook.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.List;

import cappuccino.couldbook.App;
import cappuccino.couldbook.Constants;
import cappuccino.couldbook.R;
import cappuccino.couldbook.base.BaseActivity;
import cappuccino.couldbook.core.image.ImageReader;
import cappuccino.couldbook.core.mananger.AccountM;
import cappuccino.couldbook.data.Group;
import cappuccino.couldbook.data.Result;
import cappuccino.couldbook.event.ImageReadObserver;
import cappuccino.couldbook.net.Network;
import cappuccino.couldbook.ui.Adapter.GroupAdapter;
import cappuccino.couldbook.ui.Adapter.RecylerGroupsAdapter;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/9/10.
 */
public class GroupsActivity extends BaseActivity {

    String TAG = "GroupsActivity";
    private RecyclerView show;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private Toolbar toolbar;
    int status = 0;

    @Override
    protected void bindView() {
        setContentView(R.layout.activity_groups);
        //show = (RecyclerView) findViewById(R.id.tv_group_show);
        show = (RecyclerView) findViewById(R.id.rv_rv);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        toolbar = getToolbar();

        findViewById(R.id.menu_add).setOnClickListener(listener);
        findViewById(R.id.menu_applicants).setOnClickListener(listener);
        findViewById(R.id.menu_create).setOnClickListener(listener);
        findViewById(R.id.menu_exit).setOnClickListener(listener);
        findViewById(R.id.menu_logout).setOnClickListener(listener);
        findViewById(R.id.menu_search).setOnClickListener(listener);
        findViewById(R.id.menu_edit).setOnClickListener(listener);


        if (toolbar == null){
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            setToolbar(toolbar);
            setSupportActionBar(toolbar);
            getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,toolbar, R.string.open, R.string.close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };
        mDrawerToggle.syncState();
        mDrawerLayout.addDrawerListener(mDrawerToggle);


        String name = getIntent().getStringExtra("group_name");
        if (name == null || name.isEmpty()) {
            status = 1;
            AccountM accountM = new AccountM(this);
            int id = accountM.getAccout();
            if (id == -1) {
                Toast.makeText(this, "请先登录", Toast.LENGTH_SHORT).show();
            } else {
                Network.getNetApi().getGroups(id).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(observer);
            }
        }else {
            status = 2;
            AccountM accountM = new AccountM(this);
            int id = accountM.getAccout();
            if (id == -1) {
                Toast.makeText(this, "请先登录", Toast.LENGTH_SHORT).show();
            } else {
                Network.getNetApi().getGroups(name).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(observer);
            }
        }
    }


    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.menu_add:
                    toAdd();
                    break;
                case R.id.menu_applicants:
                    toApplicant();
                    break;
                case R.id.menu_create:
                    toCreate();
                    break;
                case R.id.menu_exit:
                    finish();
                    break;
                case R.id.menu_search:
                    final EditText et_search = new EditText(GroupsActivity.this);
                    new AlertDialog.Builder(GroupsActivity.this).setTitle("请输入社团名")
                            .setView(et_search).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            toGroups(et_search.getText().toString());
                        }
                    })
                            .setNegativeButton("取消",null).show();
                    break;
                case R.id.menu_logout:
                    AccountM accountM = new AccountM(GroupsActivity.this);
                    if(accountM.getAccout() != -1){
                        accountM.clearAccout();
                        toLogin();
                        finish();
                    }
                    break;
                case R.id.menu_edit:
                    Intent intent = new Intent(GroupsActivity.this, QueryNumberActivity.class);
                    intent.putExtra("type",1);
                    intent.putExtra("userId",new AccountM(GroupsActivity.this).getAccout());
                    startActivity(intent);
                    break;
            }
        }
    };

    @Override
    protected void onRestart() {
        super.onRestart();
        AccountM accountM = new AccountM(this);
        int id = accountM.getAccout();
        if(id == -1)
        {
            Toast.makeText(this,"请先登录",Toast.LENGTH_SHORT).show();
        }else {
            Network.getNetApi().getGroups(id).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(observer);
        }
    }

    @Override
    protected void init() {

    }

    Observer<Result<List<Group>>> observer = new Observer<Result<List<Group>>>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onNext(Result<List<Group>> listResult) {
            RecylerGroupsAdapter adapter = new RecylerGroupsAdapter(GroupsActivity.this, listResult.data,status);
            show.setAdapter(adapter);
            show.setLayoutManager(new LinearLayoutManager(GroupsActivity.this));
        }
    };

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                Toast.makeText(GroupsActivity.this, "action_settings", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_logout:
                AccountM accountM = new AccountM(this);
                if(accountM.getAccout() != -1){
                    accountM.clearAccout();
                    toLogin();
                    finish();
                }
                break;
            case R.id.action_applicant:
                toApplicant();
                break;
            case R.id.action_create:
                toCreate();
                break;
            case R.id.action_search:
                final EditText et_search = new EditText(this);
                new AlertDialog.Builder(this).setTitle("请输入社团名").setIcon(android.R.drawable.ic_dialog_info)
                        .setView(et_search).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        toGroups(et_search.getText().toString());
                    }
                })
                        .setNegativeButton("取消",null).show();
                break;
            case R.id.action_add:
                toAdd();
                break;
            default:
                Toast.makeText(GroupsActivity.this, "default:"+item.getItemId(), Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
